//
// Created by 66477 on 2022/10/5.
//

#ifndef DATA_STRUCTURE_BITREE5_1_H
#define DATA_STRUCTURE_BITREE5_1_H

#include "Node5_1.h"

class BiTree5_1 {
private:
    Node5_1 *root = nullptr;

    Node5_1 *createNodeDLR(string s, int *i) {
        if (*i >= s.length()) {
            (*i)++;
            cout << "input string exception" << endl;
            return nullptr;
        } else if (s[*i] == '#') {
            (*i)++;
            return nullptr;
        } else {
            Node5_1 *node = new Node5_1(s[*i]);
            (*i)++;
            node->setLChild(createNodeDLR(s, i));
            node->setRChild(createNodeDLR(s, i));
            return node;
        }
    }

    void print(Node5_1 *node) {
        if (node == nullptr) {
            cout << "#";
        } else {
            cout << node->getData();
            print(node->getLChild());
            print(node->getRChild());
        }
    }

    int getH(Node5_1 *node, int h) {
        if (node != nullptr) {
            int a = getH(node->getLChild(), h + 1);
            int b = getH(node->getRChild(), h + 1);
            return a > b ? a : b;
        } else {
            return h;
        }
    }

public:
    BiTree5_1(string s) {
        int i;
        root = createNodeDLR(s, &i);
    }

    void print() {
        print(root);
        cout << endl;
    }

    int getHeight() {
        return getH(root, 0);
    }
};

#endif //DATA_STRUCTURE_BITREE5_1_H
